package com.njrsrc.android.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.bean.NewsInfo;
import com.njrsrc.common.utils.Helpers;

public class NewsListAdapter extends BaseAdapter {

	public ArrayList<NewsInfo> list = new ArrayList<NewsInfo>();
	private Context mContext;

	public NewsListAdapter(Context mContext) {
		this.mContext = mContext;
	}
	
	public void setData(ArrayList<NewsInfo> data) {
        if (data != null) {
            for (NewsInfo operateEntry : data) {
            	list.add(operateEntry);
            }
        }
    }

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		Holder holder = null;
		if (convertView == null) {
			holder = new Holder();
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.item_list_news, null);
			holder.newsTitleTextView = (TextView) convertView.findViewById(R.id.newsTitle);
			holder.newsDateTextView = (TextView) convertView.findViewById(R.id.newsDate);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		
		NewsInfo bean = list.get(position);
		if(bean != null)
		{
			convertView.setTag(R.id.news_type, bean.getNews_id());
			String date = Helpers.getStandardTimeStr(mContext,bean.getPublished_at());
			holder.newsTitleTextView.setText(bean.getTitle());
			holder.newsDateTextView.setText(date);
		}
		return convertView;
	}

	class Holder {
		TextView newsTitleTextView;
		TextView newsDateTextView;
	}
}
